package com.crm.service.impl.activityManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.activityManager.CustomerProductActivityMapper;
import com.crm.dao.mapper.customMapper.activityManagerCustomMapper.CustomerProductActivityCustomMapper;
import com.crm.model.entity.activityManager.CustomerProductActivity;
import com.crm.model.entity.activityManager.CustomerProductActivityExample;
import com.crm.model.qo.activityManager.CustomerProductActivityQO;
import com.crm.model.vo.activityManager.CustomerProductActivityVO;
import com.crm.service.activityManager.CustomerProductActivityDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/2/23 11:23
 */
@Service
public class CustomerProductActivityDbServiceImpl implements CustomerProductActivityDbService {

    @Autowired
    private CustomerProductActivityMapper productActivityMapper;

    @Autowired
    private CustomerProductActivityCustomMapper productActivityCustomMapper;

    /**
     * 活动管理--根据主键查询活动信息(活动查看/编辑)
     * @param activityId    ---活动主键
     * @return
     */
    @Override
    public CustomerProductActivity selectProductActivityByPrimaryKey(String activityId) {
        CustomerProductActivity customerProductActivity = productActivityMapper.selectByPrimaryKey(activityId);
        return customerProductActivity;
    }

    /**
     * 活动管理--活动列表
     * @param customerProductActivityQO ---活动列表查询条件
     * @param orderStr                  ---活动列表排序
     * @author yincuimei
     * @return
     */
    @Override
    public List<CustomerProductActivityVO> selectCustomerProductActivityVOList(CustomerProductActivityQO customerProductActivityQO, String orderStr) {
        return productActivityCustomMapper.selectCustomerProductActivityVOList(customerProductActivityQO,orderStr);
    }

    /**
     * 活动管理--添加活动
     * @param customerProductActivity
     * @return
     */
    @Override
    public Boolean insertProductActivity(CustomerProductActivity customerProductActivity) {
        Boolean result = false;
        int num = productActivityMapper.insertSelective(customerProductActivity);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 活动管理--查询活动是否重复
     * @param productActivityName
     * @param activityPeriodValidityStart
     * @param activityPeriodValidityEnd
     * @return
     */
    @Override
    public List<CustomerProductActivity> selectActivityIfRepeated(String productActivityName, Date activityPeriodValidityStart, Date activityPeriodValidityEnd) {
        CustomerProductActivityExample productActivityExample = new CustomerProductActivityExample();
        CustomerProductActivityExample.Criteria criteria1 = productActivityExample.createCriteria();
        criteria1.andProductActivityNameEqualTo(productActivityName);
        criteria1.andActivityPeriodValidityStartEqualTo(activityPeriodValidityStart);
        criteria1.andActivityPeriodValidityEndEqualTo(activityPeriodValidityEnd);
        List<CustomerProductActivity> customerProductActivities = productActivityMapper.selectByExample(productActivityExample);
        return customerProductActivities;
    }

    /**
     * 活动列表--(活动下架)根据活动ID及当前时间查询活动是否在有效期内
     * @param productActivityId ---活动ID
     * @param nowDate           ---当前时间
     * @author yincuimei
     * @return
     */
    @Override
    public Boolean selectActivityIsInValidityPeriod(String productActivityId,String nowDate) {
        return productActivityCustomMapper.selectActivityIsInValidityPeriod(productActivityId,nowDate) > 0;
    }

    /**
     * 活动列表--活动上/下架操作
     * @param productActivityId    ---活动主键
     * @param activityShelfStatus  ---上下架状态(0:上架;1:下架)
     * @param accountId            ---当前操作人账号ID
     * @author yincuimei
     * @return
     */
    @Override
    public Boolean updateActivityShelfStatus(String productActivityId,String activityShelfStatus,String accountId) {
        CustomerProductActivity customerProductActivity = new CustomerProductActivity();
        customerProductActivity.setProductActivityId(productActivityId);//活动ID
        customerProductActivity.setActivityShelfStatus(activityShelfStatus);//上下架状态
        customerProductActivity.setUpdateTm(new Date());//更新时间
        customerProductActivity.setUpdatePeopleId(accountId);//操作人账号ID
        return productActivityMapper.updateByPrimaryKeySelective(customerProductActivity) > 0;
    }

    /**
     * 活动列表--根据主键修改活动
     * @param customerProductActivity
     * @return
     */
    @Override
    public Boolean updateProductActivityByPrimaryKey(CustomerProductActivity customerProductActivity) {
        int num = productActivityMapper.updateByPrimaryKeySelective(customerProductActivity);
        if(num>0){
            return null;
        }else {
            return false;
        }
    }

    /**
     * 活动列表--修改时查询除自己之外有没有重复的活动
     * @param productActivityId
     * @param productActivityName
     * @param activityPeriodValidityStart
     * @param activityPeriodValidityEnd
     * @return
     */
    @Override
    public List<CustomerProductActivity> selectActivityIfRepeatedNotSelf(String productActivityId, String productActivityName, Date activityPeriodValidityStart, Date activityPeriodValidityEnd) {
        CustomerProductActivityExample productActivityExample = new CustomerProductActivityExample();
        CustomerProductActivityExample.Criteria criteria1 = productActivityExample.createCriteria();
        criteria1.andProductActivityIdNotEqualTo(productActivityId);
        criteria1.andProductActivityNameEqualTo(productActivityName);
        criteria1.andActivityPeriodValidityStartEqualTo(activityPeriodValidityStart);
        criteria1.andActivityPeriodValidityEndEqualTo(activityPeriodValidityEnd);
        List<CustomerProductActivity> customerProductActivities = productActivityMapper.selectByExample(productActivityExample);
        return customerProductActivities;
    }

    @Override
    public List<CustomerProductActivity> selectValidityPeriodCustomerActivityList(String searchInfo) {
        CustomerProductActivityExample productActivityExample = new CustomerProductActivityExample();
        CustomerProductActivityExample.Criteria criteria = productActivityExample.createCriteria();
        criteria.andDeleteStatusEqualTo((short) StatusCodeEnum.NORMAL_CODE.getState());
        //活动上架
        criteria.andActivityShelfStatusEqualTo("0");
        if(!StringUtils.isBlank(searchInfo)){
            criteria.andProductActivityNameLike("%" + searchInfo + "%");
        }
        criteria.andActivityPeriodValidityStartLessThanOrEqualTo(new Date());
        criteria.andActivityPeriodValidityEndGreaterThanOrEqualTo(new Date());
        productActivityExample.setOrderByClause("create_tm desc");
        List<CustomerProductActivity> customerProductActivityList = productActivityMapper.selectByExample(productActivityExample);
        return customerProductActivityList;
    }

    /***
     * @Description:任务管理-任务列表 查询近期活动列表
     * @Author: luojie
     * @Date: 2021/4/26 16:45
     */
    @Override
    public List<CustomerProductActivity> selectRecentActivityList() {
        CustomerProductActivityExample productActivityExample = new CustomerProductActivityExample();
        CustomerProductActivityExample.Criteria criteria = productActivityExample.createCriteria();
        criteria.andDeleteStatusEqualTo((short) StatusCodeEnum.NORMAL_CODE.getState());
        productActivityExample.setOrderByClause("create_tm desc");
        List<CustomerProductActivity> customerProductActivityList = productActivityMapper.selectByExample(productActivityExample);
        return customerProductActivityList;
    }
}
